#include <algorithm>
#include <cstdint>
#include <iostream>
#include <istream>
#include <vector>

using ll = int64_t;

std::vector<ll> lg2,nums;
std::vector<std::vector<ll>> st;

int main(){
    std::iostream::sync_with_stdio(false);
    std::cin.tie(nullptr);
    ll n;
    std::cin>>n;
    ll q;
    std::cin>>q;
    nums.resize(n+1);
    lg2.resize(1e5+5);
    for(ll i=2;i<1e5+5;i++){
        lg2[i]=lg2[i>>1]+1;
    }
    st.resize(n+1,std::vector<ll>(lg2[n]+2));
    for(ll i=1;i<=n;i++){
        std::cin>>nums[i];
        st[i][0]=nums[i];
    }
    
    for(ll j=1;j<lg2[n]+2;j++){
        for(ll i=1;i<=n && i+(1ll<<j)-1<=n;i++){
            st[i][j]=std::max(st[i][j-1],st[i+(1ll<<(j-1))][j-1]);
        }
    }
    while(q--){
        ll l,r;
        std::cin>>l>>r;
        ll range = r-l+1;
        ll k=lg2[range];
        std::cout<<
            std::max(st[l][k],st[r-(1ll<<k)+1][k])
        <<"\n";
    }
}